import 'package:flutter/material.dart';
import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart';
typedef DateValueCallback(DateTime dateTime, List<int> selectedIndex);
class TimeFormat{

  // 日期转换 DateTime转字符串 2019-09-08 19:08
  static String dateToString(DateTime time) {
    int minute = time.minute;
    return '${time.year}-${time.month}-${time.day} ${time.hour}:${minute < 10 ? ('0$minute') : minute}';
  }

  // 日期转换 DateTime转字符串 2019-09-08
  static String dateToString2(DateTime time) {
    return '${time.year}-${time.month < 10 ? ('0${time.month}') : time.month}-${time.day < 10 ? ('0${time.day}') : time.day}';
  }

  // 日期转换 DateTime转字符串 2019/09/08
  static String dateToString3(DateTime time) {
    return '${time.year}/${time.month}/${time.day}';
  }
  // 日期转换 DateTime转字符串 2019-09-08
  static String dateToString4(DateTime time) {
    return '${time.year}-${time.month < 10 ? ('0${time.month}') : time.month}-${time.day < 10 ? ('0${time.day}') : time.day}';
  }

  // 时间戳转字符串 2019-09-08 19:08
  static String intToString(int millisecondsSinceEpoch) {
    DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch);
    return dateToString2(dateTime);
  }

  // 时间戳转字符串2019-09-08
  static String intToString2(int millisecondsSinceEpoch) {
    DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch);
    return dateToString(dateTime);
  }

  // 秒转换成时分
  static String transformTime(int seconds){
    if (seconds == 0 || seconds < 60) {
      return '${seconds}s';
    } else{
      int hour = seconds ~/ 3600;
      int minute = (seconds % 3600) ~/ 60;
      String hourStr = hour >= 1 ? '${hour}h' : '';
      String minuteStr = minute >= 1 ? '${minute}min': '';
      return '$hourStr$minuteStr';
    }
  }

  static const String YEAR_MONTH_DAY = 'yyyy-MM-dd';
  static const String YEAR_MONTH = 'yyyy-MM';


  static void showDatePicker(BuildContext context,String dateFormat,DateValueCallback onConfirm){
    DatePicker.showDatePicker(context,
        pickerTheme: DateTimePickerTheme(
            showTitle: true,
            confirm: Text('确定',style: TextStyle(color: Colors.blue),),
            cancel: Text('取消')
        ),
        minDateTime: DateTime.parse('2010-01-01'),
        maxDateTime: DateTime.now(),
        initialDateTime: DateTime.now(),
        dateFormat: dateFormat,
//      dateFormat: 'yyyy-MM-dd',
        locale: DateTimePickerLocale.zh_cn,//语言
//      onCancel: onCancel,
        onConfirm: onConfirm
    );
  }

}